package com.blog.backend.service.impl;

import com.blog.backend.config.CodeMsg;
import com.blog.backend.config.RedisValueConfig;
import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.FoodMapper;
import com.blog.backend.mapper.ImageMapper;
import com.blog.backend.req.FoodReq;
import com.blog.backend.req.ListFoodReq;
import com.blog.backend.resp.ArticleResp;
import com.blog.backend.resp.DoorHotFoodResp;
import com.blog.backend.resp.FoodResp;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.service.FoodService;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.RedisUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public Boolean insertFood(FoodReq foodReq) {
        log.info("新增食品名称：{}",foodReq.getFoodName());
        foodReq.setFoodId(UuidUtils.getId());
        foodMapper.insertFood(foodReq);
        return true;
    }

    @Override
    public PageResult<FoodResp> selectFoodList(ListFoodReq req) {
        log.info("查询食品列表：{}",req.getType());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<FoodResp> list=foodMapper.selectFoodList(req);
        //查询一张图片
        this.getImage(list);
        PageInfo<FoodResp> info=new PageInfo<FoodResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public FoodResp selectFoodDetail(String foodId) {
        log.info("查看食品详情：{}",foodId);
        FoodResp resp=foodMapper.selectFoodDetail(foodId);
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        if (StringUtils.isNotBlank(resp.getFoodImg())){
            List<PictureUrlResp> strings = imageMapper.selectImageList(resp.getFoodImg());
            resp.setImageList(strings);
        }
        if (redisUtils.hasKey(RedisValueConfig.article_pre + foodId)){
            Integer browse = (Integer) redisUtils.get(RedisValueConfig.article_pre + foodId);
            resp.setBrowseNum(browse);
        }
        return resp;
    }

    @Override
    public Boolean updateFoodDetail(FoodReq foodReq) {
        log.info("修改食品详情内容：{},{}",foodReq.getFoodId(),foodReq.getFoodName());
        FoodResp resp=foodMapper.selectFoodDetail(foodReq.getFoodId());
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        foodMapper.updateFoodDetail(foodReq);
        return true;
    }

    @Override
    public Boolean deleteFood(String foodId) {
        FoodResp resp=foodMapper.selectFoodDetail(foodId);
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        foodMapper.updateFoodStatus(foodId);
        //同步删除相关图片
        imageMapper.updateImageStatus(resp.getFoodImg());
        return true;
    }

    @Override
    public DoorHotFoodResp getHotFoodList() {
        DoorHotFoodResp resp = new DoorHotFoodResp();
        log.info("查询热门美食");
        List<FoodResp> list=foodMapper.selectHotFoodList();
        if (!CollectionUtils.isEmpty(list)){
            for (FoodResp foodResp : list) {
                if (StringUtils.isNotBlank(foodResp.getFoodImg())){
                    PictureUrlResp image= imageMapper.selectImage(foodResp.getFoodImg());
                    List<PictureUrlResp> imageList=new ArrayList<>();
                    imageList.add(image);
                    foodResp.setImageList(imageList);
                }
            }
            resp.setFoodResp(list.get(0));
            //list中去除第一个
            list.remove(0);
            resp.setFoodRespList(list);
        }
        return resp;
    }

    @Override
    public PageResult<FoodResp> getDoorFoodList(ListFoodReq request) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<FoodResp> list=foodMapper.selectDoorFoodList(request);
        //查询一张图片
        this.getImage(list);
        PageInfo<FoodResp> info=new PageInfo<FoodResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }


    private void getImage(List<FoodResp> list ){
        if (!CollectionUtils.isEmpty(list)){
            for (FoodResp foodResp : list) {
                if (StringUtils.isNotBlank(foodResp.getFoodImg())){
                    PictureUrlResp image= imageMapper.selectImage(foodResp.getFoodImg());
                    List<PictureUrlResp> imageList=new ArrayList<>();
                    imageList.add(image);
                    foodResp.setImageList(imageList);
                }
            }
        }
    }

}
